import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-10-25
 * Time: 2:42
 */
public class Demo3 {
    public static void main(String[] args) throws SQLException {
        Scanner sc = new Scanner(System.in);
        //1. 创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java1025?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //2.与数据库建立连接
        Connection connection = dataSource.getConnection();

        //3. 构造sql
        System.out.println("请输入你要修改的数据id：");
        int id = sc.nextInt();

        String sql = "update student set name = '修改' where id = ?;";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);

        //4. 将sql发送给MySQL数据库进行执行
        int a = statement.executeUpdate();

        //5. 资源释放
        statement.close();
        connection.close();
    }
}
